package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageBean;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddArticleDto;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddCateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_article")
@Slf4j
public class YddArticleController {

    @Autowired
    IYddArticleService iYddArticleService;
    @Autowired
    IYddCateService iYddCateService;

    /**
     * 文章列表分页查询
     * @param start_time
     * @param end_time
     * @param title
     * @param cateid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String start_time, String end_time, String title, String cateid, int page, int limit) {

        //1.构建分页查询器
        Page<YddArticle> pageInfo = new Page<>(page, limit);
        //2.创建查询器
        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
        //3.添加查询条件
        queryWrapper.between(start_time != null && start_time != "" && end_time != null && end_time != "",
                YddArticle::getCreateDate, start_time, end_time);
        queryWrapper.like(title != null && title != "", YddArticle::getTitle, title);
        queryWrapper.eq(cateid != null && cateid != "",YddArticle::getCateid,cateid);
        queryWrapper.orderByDesc(YddArticle::getCreateDate);//按创建时间降序;
        //4.查询结果
        iYddArticleService.page(pageInfo,queryWrapper);
        List<YddArticle> records = pageInfo.getRecords();
        List<YddArticleDto> yddArticleDtos = new ArrayList<>();
        for (YddArticle record : records) {
            YddArticleDto yddArticleDto = new YddArticleDto();
            //将数据拷贝到dto中去
            BeanUtils.copyProperties(record,yddArticleDto);
            Integer cateid1 = record.getCateid();
            //查询分类表中分类名称;
            YddCate cate = iYddCateService.getById(cateid1);
            //设置分类名称
            yddArticleDto.setCatename(cate.getCatename());
            //将处理好的数据添加到集合中去
            yddArticleDtos.add(yddArticleDto);
        }
        //5.将查询出来的结果数据封装到自定义的分页对象中去
        PageBean<YddArticleDto> pageBean = new PageBean<>();
        pageBean.setCurrentPage(pageInfo.getCurrent());//当前页
        pageBean.setPageSize(pageInfo.getSize());//每页显示条数
        pageBean.setCount(pageInfo.getTotal());//总记录数
        pageBean.setTotalPages(pageInfo.getPages());//总页数
        pageBean.setData(yddArticleDtos);//数据集合

        return ResultData.ok("",pageBean);
    }

    /**
     * 根据id删除文章
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData deleteById(Long id){
        iYddArticleService.removeById(id);
        return ResultData.ok("文章删除成功!",null);
    }

    /**
     * 添加文章
     * @param yddArticle
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddArticle yddArticle){
        iYddArticleService.save(yddArticle);
        return ResultData.ok("文章添加成功",null);
    }

    /**
     * 文章详情
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id){
        if (id == null){
            return ResultData.error("请提供需要查看的文章id");
        }
        YddArticle article = iYddArticleService.getById(id);
        if(article == null){
            return ResultData.error("您查找的文章不存在");
        }
        return ResultData.ok("",article);
    }

    /**
     * 修改文章
     * @param yddArticle
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddArticle yddArticle){
        if (yddArticle.getId() == null){
            return ResultData.error("请选择要修改的文章");
        }
        //设置修改时间毫秒值
        yddArticle.setUpdateDate(System.currentTimeMillis());
        iYddArticleService.updateById(yddArticle);
        return ResultData.ok("文章修改成功",null);
    }
}
